﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using Entity;

namespace CustomerManager
{
    public partial class ucReport : UserControl
    {
        int status;
        public FrmMain frm;
        DataTable dtCaNhan;
        DataTable dtDoanhNghiep;

        public ucReport()
        {
            InitializeComponent();
        }
        
        public void LoadCustomer(int s)
        {
            status = s;
            if (status == -1)
            {
                dtCaNhan = PersonalBUS.LayTatCaKHCaNhan();
                dgvCaNhan.DataSource = dtCaNhan;
                dtDoanhNghiep = CompanyBUS.LayTatCaKHDoanhNghiep();
                dgvDoanhNghiep.DataSource = dtDoanhNghiep;

                DataTable dt = NhanVienBUS.LayNhanVienTheoID(status);
                DataRow dr = dt.NewRow();
                dr["Tennv"] = "Tất cả Nhân Viên";
                dr["Manv"] = -1;
                dt.Rows.Add(dr);
                cmbNhanVien.DataSource = dt;
                cmbNhanVien.DisplayMember = "Tennv";
                cmbNhanVien.ValueMember = "Manv";
                cmbNhanVien.SelectedIndex = cmbNhanVien.Items.Count - 1;

            }
            else
            {
                cmbNhanVien.Visible = false;
                dgvCaNhan.DataSource = PersonalBUS.LayKHCaNhanTheoNhanVien(status);
                dgvDoanhNghiep.DataSource = CompanyBUS.LayKHDoanhNghiepTheoNhanVien(status);
            }

            dgvCaNhan.BringToFront();
            cmbLoaiKhachHang.SelectedIndex = 0;
            cmbTieuChiCaNhan.SelectedIndex = 0;
            cmbNhanVien_SelectedIndexChanged(new object(), new EventArgs());
            cmbTieuChiCaNhan_SelectedIndexChanged(new object(), new EventArgs());
        }
        public void loadfrmmain()
        {
            frm = (FrmMain)Application.OpenForms["FrmMain"];
        }
        private void cmbLoaiKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbLoaiKhachHang.SelectedIndex == 0)
                dgvCaNhan.BringToFront();
            else
                dgvDoanhNghiep.BringToFront();
            txtTimKiem.Clear();
            }
        string filterNV = "";
        string filterTCCN = "";
        string filterTCDN = "";
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            if (cmbLoaiKhachHang.SelectedIndex == 0)
            {
                dtDoanhNghiep.DefaultView.RowFilter = filterNV;
                dtCaNhan.DefaultView.RowFilter = filterNV + filterTCCN + txtTimKiem.Text + "%'";
            }
            else
            {
                dtDoanhNghiep.DefaultView.RowFilter = filterNV + filterTCDN + txtTimKiem.Text + "%'";
                dtCaNhan.DefaultView.RowFilter = filterNV;
            }
        }

        private void cmbNhanVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbNhanVien.SelectedIndex == cmbNhanVien.Items.Count - 1)
                    filterNV = "";
                else
                    filterNV = "MaNhanVien = " + cmbNhanVien.SelectedValue;
                dtCaNhan.DefaultView.RowFilter = filterNV;
                dtDoanhNghiep.DefaultView.RowFilter = filterNV;
                txtTimKiem.Clear();
            }
            catch { }
        }

        private void cmbTieuChiCaNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbTieuChiCaNhan.SelectedIndex == 0)
                {
                    filterTCCN = "Makhcanhan like '%";
                    filterTCDN = "Makhdoanhnghiep like '%";
                }
                else if (cmbTieuChiCaNhan.SelectedIndex == 1)
                {
                    filterTCCN = "Tenkhcanhan like '%";
                    filterTCDN = "Tendoanhnghiep like '%";
                }
                else
                {
                    filterTCCN = filterTCDN = "masothue like '%";
                }
                txtTimKiem.Clear();
            }
            catch { }
        }

        private void btnInThongTin_Click(object sender, EventArgs e)
        {
            DataTable dt;

            if (cmbLoaiKhachHang.SelectedIndex == 0)
            {
                rptPersonal rpt = new rptPersonal();
                dt = DebtBUS.LayKhoanVay(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colMa"].Value.ToString(), cmbLoaiKhachHang.SelectedIndex);
                rpt.SetDataSource(dt);
                rpt.SetParameterValue("MaKhachHang", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colMa"].Value.ToString());
                rpt.SetParameterValue("TenKhachHang", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colTen"].Value.ToString());
                rpt.SetParameterValue("DiaChi", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colDiaChi"].Value.ToString());
                rpt.SetParameterValue("HoKhau", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colHoKhau"].Value.ToString());
                rpt.SetParameterValue("NgaySinh", DateTime.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colNgaySinh"].Value.ToString()));
                rpt.SetParameterValue("CMND", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colCMND"].Value.ToString());
                rpt.SetParameterValue("DienThoai", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colDienThoai"].Value.ToString());
                rpt.SetParameterValue("MaSoThue", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colMaSoThue"].Value.ToString());
                rpt.SetParameterValue("NgheNghiep", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colNgheNghiep"].Value.ToString());
                rpt.SetParameterValue("NoiLamViec", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colNoiLamViec"].Value.ToString());
                rpt.SetParameterValue("ThuNhapBinhQuan", Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colThuNhapBinhQuan"].Value.ToString()));
                rpt.SetParameterValue("HonNhan", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colHonNhan"].Value.ToString());
                rpt.SetParameterValue("ThuNhapGiaDinh", Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colThuNhapGiaDinh"].Value.ToString()));
                rpt.SetParameterValue("SoNguoiPhuThuoc", Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colSoLuongPhuThuoc"].Value.ToString()));
                rpt.SetParameterValue("TrinhDo", dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colTrinhDo"].Value.ToString());
                rpt.SetParameterValue("SoNamLamViec", Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colSoNamLamViec"].Value.ToString()));
                rpt.SetParameterValue("HanMuc", Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colHanMuc"].Value.ToString()));

                ucrpt f = new ucrpt();
                f.rptViewer.ReportSource = rpt;
                loadfrmmain();
                frm.Controls.Add(f);
                f.Dock = DockStyle.Fill;
                f.BringToFront();
            }
            else
            {
                rptCompany rpt = new rptCompany();
                dt = DebtBUS.LayKhoanVay(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colMaDoanhNghiep"].Value.ToString(), cmbLoaiKhachHang.SelectedIndex);
                rpt.SetDataSource(dt);

                rpt.SetParameterValue("makhdoanhnghiep", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colMaDoanhNghiep"].Value.ToString());
                rpt.SetParameterValue("TenDoanhNghiep", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTenDoanhNghiep"].Value.ToString());
                rpt.SetParameterValue("DienThoai", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colDienThoaiDN"].Value.ToString());
                rpt.SetParameterValue("DiaChi", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colDiaChiDN"].Value.ToString());
                rpt.SetParameterValue("Fax", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colFax"].Value.ToString());
                rpt.SetParameterValue("MaSoThue", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colMaSoThueDN"].Value.ToString());
                rpt.SetParameterValue("QuyetDinhThanhLap", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colQuyetDinhThanhLap"].Value.ToString());
                rpt.SetParameterValue("NgayQuyetDinh", DateTime.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNgayQuyetDinh"].Value.ToString()));
                rpt.SetParameterValue("CoQuanCap", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colCoQuanCap"].Value.ToString());
                rpt.SetParameterValue("CoQuanQlyTrucTiep", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colCoQuanQLyTrucTiep"].Value.ToString());
                rpt.SetParameterValue("LoaiHinh", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colLoaiHinh"].Value.ToString());
                rpt.SetParameterValue("NganhKinhDoanh", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNganhKinhDoanh"].Value.ToString());
                rpt.SetParameterValue("GiayPhepKinhDoanh", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colGiayPhepKinhDoanh"].Value.ToString());
                rpt.SetParameterValue("NgayCapGiayPhep", DateTime.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNgayCapGiayPhep"].Value.ToString()));
                rpt.SetParameterValue("NganhKinhTe", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNganhKinhTe"].Value.ToString());
                rpt.SetParameterValue("ChuTichHDQT", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTenChuTichHDQT"].Value.ToString());
                rpt.SetParameterValue("NamSinhCTHDQT", DateTime.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNamSinhChuTichHDQT"].Value.ToString()));
                rpt.SetParameterValue("TongGiamDoc", dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTenTongGiamDoc"].Value.ToString());
                rpt.SetParameterValue("NamSinhTGD", DateTime.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNamSinhTongGiamDoc"].Value.ToString()));
                rpt.SetParameterValue("TongSoLaoDong", Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTongSoLaoDong"].Value.ToString()));
                rpt.SetParameterValue("VonDieuLe", Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colVonDieuLe"].Value.ToString()));
                rpt.SetParameterValue("VonKinhDoanh", Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colVonKinhDoanh"].Value.ToString()));
                rpt.SetParameterValue("HanMuc", Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colHanMucDN"].Value.ToString()));

                ucrpt f = new ucrpt();
                f.rptViewer.ReportSource = rpt;
                loadfrmmain();
                frm.Controls.Add(f);
                f.Dock = DockStyle.Fill;
                f.BringToFront();
            }
        }
        
        private void btnDuNo_Click(object sender, EventArgs e)
        {
            
            DataTable dt;
            rptDuNo rpt = new rptDuNo();
            rpt.SetDataSource(DebtBUS.BaoCaoDuNoLon());

            ucrpt f = new ucrpt();
            f.rptViewer.ReportSource = rpt;
            loadfrmmain();
            frm.Controls.Add(f);
            f.Dock = DockStyle.Fill;
            f.BringToFront();
        }
    }
}
